﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditUnitTrustPurchaseModel
    {
        public UnitTrustTransactionScreenData ScreenData { get; set; }
        public UnitTrustHeader Header { get; set; }
        public UnitTrustTransaction Transaction { get; set; }
        public EditUnitTrustPurchaseModel()
        {
            if (null == Header)
                Header = new UnitTrustHeader();
            if (null == Transaction)
                Transaction = new UnitTrustTransaction();
            var defaultItem = WebUtil.SelectListSelectDefault();
            TransactionTypeList = new BindingList<SelectListItem>();
            TypeOfPaymentList = new BindingList<SelectListItem> { defaultItem };
            PhysicalList = new BindingList<SelectListItem> { defaultItem };
            KeepAtList = new BindingList<SelectListItem> { defaultItem };
        }

        public static EditUnitTrustPurchaseModel BuildModel(UnitTrustTransactionScreenData data)
        {
            var model = new EditUnitTrustPurchaseModel();
            if (data.HeaderDTO.Header != null)
                model.Header = data.HeaderDTO.Header;
            model.ScreenData = data;
            if (data.Transaction != null)
            {
                model.Transaction = data.Transaction;
                model.UpdateMode = true;
            }
            else
            {
                model.SelectedTransactionType = "Purchase";
            }
            //fixed value ตัวเลือก "Purchase", "Subscription"  Default = Purchase
            model.TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Purchase", Value = "Purchase" });
            model.TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Subscription", Value = "Subscription" });
            

            //fixed value  ตัวเลือก "Scrip", "scripless"
            model.PhysicalList.Add(item: new SelectListItem() { Selected = false, Text = "scrip", Value = "scrip" });
            model.PhysicalList.Add(item: new SelectListItem() { Selected = false, Text = "scripless", Value = "scripless" });

            //fixed value  ตัวเลือก "Custodian", "OIC", "Safe"
            model.KeepAtList.Add(item: new SelectListItem() { Selected = false, Text = "Custodian", Value = "Custodian" });
            model.KeepAtList.Add(item: new SelectListItem() { Selected = false, Text = "OIC", Value = "OIC" });
            model.KeepAtList.Add(item: new SelectListItem() { Selected = false, Text = "Safe", Value = "Safe" });

            if (data.PaymentTypes != null || data.PaymentTypes.Any())
            {
                foreach (var selectListItem in data.PaymentTypes)
                {
                    model.TypeOfPaymentList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.PaymentTypeName, Value = selectListItem.PaymentTypeId.ToString() });
                }
            }
            model.HeaderID = model.Header.ID;
            model.TransactionID = model.Transaction.ID;

            return model;
        }


        #region dropdownlist data
        //TransactionType
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedTransactionType
        {
            get { return Transaction.TransactionType; }
            set { Transaction.TransactionType = value; }
        }
        public IList<SelectListItem> TransactionTypeList { get; set; }

        //Type of Payment (.TYPEOFPAYMENT_NAME)
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedTypeOfPayment
        {
            get { return Transaction.TypeofpaymentName; }
            set { Transaction.TypeofpaymentName = value; }


        }
        public IList<SelectListItem> TypeOfPaymentList { get; set; }

        //Physical
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedPhysical
        {
            get { return Transaction.Physical; }
            set { Transaction.Physical = value; }
        }
        public IList<SelectListItem> PhysicalList { get; set; }

        //Keep at 
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedKeepAt
        {
            get { return Transaction.KeepAT; }
            set { Transaction.KeepAT = value; }
        }
        public IList<SelectListItem> KeepAtList { get; set; }

        #endregion


        #region adapter data
        public int HeaderID
        {
            get
            {
                return Transaction.UnitTrustTransactionHeaderID;
            }

            set
            {
                Transaction.UnitTrustTransactionHeaderID = value;
            }
        }
        public int TransactionID
        {
            get
            {
                return Transaction.ID;
            }

            set
            {
                Transaction.ID = value;
            }
        }


        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Maturity Date")]
        public DateTime? HeaderMaturityDate
        {
            get { return Header.MaturityDate; }
            set { Header.MaturityDate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public string RefNo
        {
            get { return Transaction.RefNO; }
            set { Transaction.RefNO = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Trade Date")]
        public DateTime? TradeDate
        {
            get { return Transaction.TradeDate; }
            set { Transaction.TradeDate = value; }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Settlement Date")]
        public DateTime? SettlementDate
        {
            get { return Transaction.SettlementDate; }
            set { Transaction.SettlementDate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? Unit
        {
            get { return Transaction.Unit; }
            set { Transaction.Unit = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? PriceUnit
        {
            get { return Transaction.PriceUnit; }
            set { Transaction.PriceUnit = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? ComRate
        {
            get { return Transaction.ComRate; }
            set { Transaction.ComRate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? VatRate
        {
            get { return Transaction.VatRate; }
            set { Transaction.VatRate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? WHTaxRate
        {
            get { return Transaction.WHTaxRate; }
            set { Transaction.WHTaxRate = value; }
        }


        public decimal? GrossAmount
        {
            get { return Transaction.GrossAmount; }
            set { Transaction.GrossAmount = value; }
        }
        public decimal? ComAmount
        {
            get { return Transaction.ComAmount; }
            set { Transaction.ComAmount = value; }
        }
        public decimal? VatAmount
        {
            get { return Transaction.VatAmount; }
            set { Transaction.VatAmount = value; }
        }
        public decimal? PurchaseValueNet
        {
            get { return Transaction.PurchaseValueNet; }
            set { Transaction.PurchaseValueNet = value; }
        }
        public decimal? WHTaxAmount
        {
            get { return Transaction.WHTaxAmount; }
            set { Transaction.WHTaxAmount = value; }
        }
        public decimal? NetSettle
        {
            get { return Transaction.NetSettle; }
            set { Transaction.NetSettle = value; }
        }
        #endregion


        public String SubmitBtn { get; set; }
        public String CancelBtn { get; set; }
        public bool UpdateMode { get; set; }

    }
}